package middleware

import (
	"log"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

// 定义违禁词和非法字符
var forbiddenWords = []string{"违规", "暴力", "恐怖", "色情"}

// VideoUploadSafeMiddleware 视频上传内容安全中间件
func VideoUploadSafeMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 从请求中获取文件
		file, err := c.FormFile("file")
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "文件未找到"})
			c.Abort()
			return
		}

		filename := file.Filename

		// 检查是否包含违禁词
		for _, word := range forbiddenWords {
			if strings.Contains(filename, word) {
				log.Printf("[内容安全] 发现违禁词: %s，文件名: %s", word, filename)
				c.JSON(http.StatusBadRequest, gin.H{"error": "文件名包含违规内容"})
				c.Abort()
				return
			}
		}

		// 一切正常，继续处理请求
		c.Next()
	}
}
